%{
#include "gtree.h"
#define YYSTYPE GTNode*
#include "syntax.tab.hpp"
#include<vector>
#include"symtab.h"
extern vector<Field> fields;
extern void print(Field f);

%}
%option yylineno


INTEGER ([0-9]+)|(0[xX][0-9a-zA-Z]+)
FLOAT   [0-9]+\.[0-9]*([eE][\+\-]?[0-9]+)?
ID [a-z_A-Z][a-zA-Z_0-9]*
SPACE [ \t\r]*
EOL \n
SEMI ;
COMMA ,
ASSIGNOP =
RELOP >|<|>=|<=|==|!=
STAR \*
DIV \/
AND &&
OR \|\|
DOT \.
NOT !
LP \(
RP \)
LB \[
RB \]
LC \{
RC \}
%%
"//" { char c=yyinput();while(c!='\n') c=yyinput();}
"/*" { while(1){
    if(yyinput()=='*'&&yyinput()=='/')
        break;
}}
int|float  { yylval=newNode(TYPE,yytext,0);  return TYPE;}
struct { yylval=newNode(STRUCT,yytext,0); return STRUCT;}
return { yylval=newNode(RETURN,yytext,0);  return RETURN;}
if	{ yylval=newNode(IF,yytext,0);  return IF;}
else	{ yylval=newNode(ELSE,yytext,0);  return ELSE;}
while	{ yylval=newNode(WHILE,yytext,0);  return WHILE;}
\+		{ yylval=newNode(PLUS,yytext,0);  return PLUS;}
\-		{ yylval=newNode(MINUS,yytext,0);  return MINUS;}
";"		{ yylval=newNode(SEMI,yytext,0);  return SEMI;}
","		{ yylval=newNode(COMMA,yytext,0);  return COMMA;}
"="		{ yylval=newNode(ASSIGNOP,yytext,0);  return ASSIGNOP;}

{FLOAT}	{ yylval=newNode(FLOAT,yytext,0);  return FLOAT;}
{INTEGER} { yylval=newNode(INT,yytext,0);  return INT;}

{ID}	{ yylval=newNode(ID,yytext,0);  return ID;}
{SPACE} {}
{EOL} {}

{RELOP}	{ yylval=newNode(RELOP,yytext,0);  return RELOP;}

\*		{ yylval=newNode(STAR,yytext,0);  return STAR;}
\/		{ yylval=newNode(DIV,yytext,0);  return DIV;}
&&	{ yylval=newNode(AND,yytext,0);  return AND;}
"||"	{ yylval=newNode(OR,yytext,0);  return OR;}

"."		{ yylval=newNode(DOT,yytext,0);  return DOT;}
"!"		{ yylval=newNode(NOT,yytext,0);  return NOT;}
"("		{ yylval=newNode(LP,yytext,0);  return LP;}
")"		{ yylval=newNode(RP,yytext,0);  return RP;}
"["		{ yylval=newNode(LB,yytext,0);  return LB;}
"]"		{ yylval=newNode(RB,yytext,0);  return RB;}
"{"		{ yylval=newNode(LC,yytext,0); Field f;fields.push_back(f);  return LC;}
"}"		{ yylval=newNode(RC,yytext,0); fields.pop_back(); return RC;}
.       { printf("Error type A at line %d: mysterious character '%s'\n",yylineno,yytext);exit(-1);}
%%
